package cn.lyjuan.archetypes.bms.web.taglib;

import cn.lyjuan.archetypes.bms.web.property.SysProperties;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;

public abstract class BaseTag implements Tag
{
    protected Tag parent;
    protected PageContext pageContext;
    protected SysProperties sysProperties;

    protected static SysProperties sysProperties()
    {
        return (SysProperties) ctx().getAttribute(SysProperties.SPRING_NAME);
    }

    protected static ServletContext ctx()
    {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

        return request.getSession().getServletContext();
    }

    protected static ServletRequest req()
    {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

        return request;
    }

    @Override
    public void setPageContext(PageContext pc)
    {
        this.pageContext = pc;
    }

    @Override
    public void setParent(Tag t)
    {
        this.parent = t;
    }

    @Override
    public Tag getParent()
    {
        return this.parent;
    }

    @Override
    public void release()
    {
        this.parent = null;
        this.pageContext = null;
        this.sysProperties = null;
    }
}
